﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DynamicListView
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            DataContext = new MainWindowViewModel();
            InitializeComponent();
        }

        private void AddValue_Click(object sender, RoutedEventArgs e)
        {
            var dataContext = (MainWindowViewModel)DataContext;
            Dispatcher.Invoke(() =>
            {
                dataContext.Data.Add(new DynamicListViewTest()
                {
                    Time = DateTime.Now,
                    DynamicData = new List<double> { 1, 2, 3 }
                });
            });

        }

        private void RemoveAColumn_Click(object sender, RoutedEventArgs e)
        {
            // 拿到对应的GridView
            var gridView = (GridView)DynamicListViewControl.View;
            
            // 移除第一列
            gridView.Columns.RemoveAt(1);
        }

        private void AddAColumn_Click(object sender, RoutedEventArgs e)
        {
            // 拿到对应的GridView
            var gridView = (GridView)DynamicListViewControl.View;

            var binding = new Binding()
            {
                Path = new PropertyPath("DynamicData[0]"),
                StringFormat = "重新添加的########"
            };

            // 创新新的GridViewColumn
            var gridViewColumn = new GridViewColumn() { Header = "新的A", DisplayMemberBinding = binding, Width = 150 };
            // 添加到GridView中
            gridView.Columns.Insert(1,gridViewColumn);
        }
    }
}
